﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class TailingpondReposity : Reposity
    {
        public IEnumerable<Tailingpond> ListTailingponds()
        {
            var tailingponds = from t in _dataContext.Tailingponds
                            select t;
            foreach (Tailingpond t in tailingponds)
            {
                yield return t;
            }
        }

        public IEnumerable<Tailingpond> QueryTailingponds(int factoryId, string name)
        {
            name = string.IsNullOrEmpty(name) ? "" : name;

            var tailingponds = from t in _dataContext.Tailingponds
                           where t.Name.Contains(name) &&
                           ((factoryId > 0) ? t.FactoryId == factoryId : 1 == 1)
                           select t;
            foreach (Tailingpond t in tailingponds)
            {
                yield return t;
            }
        }

        public Tailingpond GetTailingpondById(long id)
        {
            var tailingpond = from t in _dataContext.Tailingponds
                          where id == t.Id
                          select t;

            return tailingpond.First();
        }

        public Tailingpond CreateTailingpond(Tailingpond tailingpond)
        {
            tailingpond.CreateTime = DateTime.Now;
            tailingpond.ModifyTime = DateTime.Now;
            _dataContext.Tailingponds.InsertOnSubmit(tailingpond);
            SubmitChanges();
            return tailingpond;
        }

        public bool DeleteTailingpond(Tailingpond tailingpond)
        {
            _dataContext.Tailingponds.DeleteOnSubmit(tailingpond);
            SubmitChanges();
            return true;
        }

        public Tailingpond EditTailingpond(Tailingpond tailingpond)
        {
            tailingpond.ModifyTime = DateTime.Now;
            SubmitChanges();
            return tailingpond;
        }
    }
}